package spec.chenli;

import com.mysql.jdbc.ConnectionFeatureNotAvailableException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.SOAPConnectionFactory;
import java.io.IOException;
import java.sql.*;

//@WebServlet(name = "DeleteServlet")
public class DeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // request.setCharacterEncoding("utf-8");
        //  response.setCharacterEncoding("utf-8");
       // response.setContentType("text/html;charser=utf8");
        int id = Integer.valueOf(request.getParameter("id"));
        //获取Id从页面传递过来的id，是String类型的，需要转换。
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = null;
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/1613580007_1?characterEncoding=utf8", "root", "123456789");
            String sql = "DELETE FROM  t_news WHERE  id=?";
            //多次执行.
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
            //executeUpdate 用于删除，增加表示修改表中的一列或者多列。返回值是一个整数。
            ps.close();
            con.close();
            response.sendRedirect("/listnew");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        }
    }
